package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.common.constants.UserConstants;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
public class ApUserRealnameServiceImpl implements ApUserRealnameService {

    @Autowired
    private ApUserRealnameMapper apUserRealnameMapper;

    @Autowired
    private IWemediaClient iWemediaClient;

    @Autowired
    private ApUserMapper apUserMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ResponseResult updateStatus(AuthDto dto,Short status) {
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUserRealname apUserRealname = apUserRealnameMapper.selectById(dto.getId());
        if (apUserRealname == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"审核记录不存在");
        }

        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus(status);
        if (StringUtils.isNotBlank(dto.getMsg())){
            apUserRealname.setReason(dto.getMsg());
        }
        apUserRealnameMapper.updateById(apUserRealname);

        if (status == UserConstants.PASS_AUTH){
            //需要创建自媒体账户，实际上就是在wmUser中，添加一条信息，信息来源就是
            String name = apUserRealname.getName();
            Map<String,String> params = new HashMap<>();
            params.put("name",name);
            ResponseResult responseResult = iWemediaClient.addUser(params);
            if (responseResult == null || responseResult.getCode() != 200){
                throw  new RuntimeException("创建自媒体账户失败");
            }
            //如果创建成功了，就得更新一下用户的标签
            ApUser apUser = apUserMapper.selectById(dto.getId());
            apUser.setFlag((short) 1);
            int update = apUserMapper.updateById(apUser);
            if (update < 1){
                throw  new RuntimeException("更新用户状态失败");
            }
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 按照状态分页查询用户列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(AuthDto dto) {
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();

        Page<ApUserRealname> page = new Page<>(dto.getPage(),dto.getSize());

        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        if (dto.getStatus() != null){
            queryWrapper.eq(ApUserRealname::getStatus,dto.getStatus());
        }
        Page<ApUserRealname> pageResult = apUserRealnameMapper.selectPage(page, queryWrapper);
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)pageResult.getTotal());
        responseResult.setData(pageResult.getRecords());
        return responseResult;
    }
}
